﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Hook.Graphics;
using Microsoft.Xna.Framework.Graphics;
using Hook.Environmental.Props;
using System.IO;
using Microsoft.Xna.Framework.Media;

namespace Hook.Environmental
{
    public class Level
    {
        public List<Prop> Props;
        public Prop LevelStart, LevelFinish;
        public int NumberOfRows, NumberOfColumns;
        public string LevelMusic;

        public Level()
        {
            Props = new List<Prop>();
        }

        public Level(string FileName)
        {
            Props = new List<Prop>();
            LoadLevel(FileName);
        }

        public void Update(GameTime gt)
        {
            foreach (Prop P in Props)
            {
                P.Update(gt);
            }
        }

        public void Draw(SpriteBatch sb, Camera Cam)
        {
            foreach (Prop P in Props)
            {
                P.Draw(sb, Cam);
            }
        }

        public void LoadLevel(string FileName)
        {
            string LoadName = "Levels/" + FileName;

            if (File.Exists(LoadName))
            {
                List<string> Lines = new List<string>();
                StreamReader SR = new StreamReader(LoadName);

                string CurrentLine = SR.ReadLine();
                while (CurrentLine != null)
                {
                    Lines.Add(CurrentLine);
                    CurrentLine = SR.ReadLine();
                }

                for (int s = 0; s < Lines.Count; s++)
                {
                    if (Lines[s].StartsWith("LevelStart = "))
                    {
                        Vector3 PositionVector = new Vector3();
                        int XLoc, YLoc, ZLoc;
                        XLoc = Lines[s + 1].IndexOf('X');
                        YLoc = Lines[s + 1].IndexOf('Y');
                        ZLoc = Lines[s + 1].IndexOf('Z');
                        PositionVector.X = float.Parse(Lines[s + 1].Substring(XLoc + 2, YLoc - XLoc - 3));
                        PositionVector.Y = float.Parse(Lines[s + 1].Substring(YLoc + 2, ZLoc - YLoc - 3));
                        PositionVector.Z = float.Parse(Lines[s + 1].Substring(ZLoc + 2, Lines[s + 1].Length - ZLoc - 3));

                        Vector3 RotationVector = new Vector3();
                        XLoc = Lines[s + 2].IndexOf('X');
                        YLoc = Lines[s + 2].IndexOf('Y');
                        ZLoc = Lines[s + 2].IndexOf('Z');
                        RotationVector.X = float.Parse(Lines[s + 2].Substring(XLoc + 2, YLoc - XLoc - 3));
                        RotationVector.Y = float.Parse(Lines[s + 2].Substring(YLoc + 2, ZLoc - YLoc - 3));
                        RotationVector.Z = float.Parse(Lines[s + 2].Substring(ZLoc + 2, Lines[s + 2].Length - ZLoc - 3));

                        LevelStart = new Prop("LevelStart.txt", PositionVector, RotationVector);
                        Props.Add(LevelStart);
                    }
                    else if (Lines[s].StartsWith("LevelFinish = "))
                    {
                        Vector3 PositionVector = new Vector3();
                        int XLoc, YLoc, ZLoc;
                        XLoc = Lines[s + 1].IndexOf('X');
                        YLoc = Lines[s + 1].IndexOf('Y');
                        ZLoc = Lines[s + 1].IndexOf('Z');
                        PositionVector.X = float.Parse(Lines[s + 1].Substring(XLoc + 2, YLoc - XLoc - 3));
                        PositionVector.Y = float.Parse(Lines[s + 1].Substring(YLoc + 2, ZLoc - YLoc - 3));
                        PositionVector.Z = float.Parse(Lines[s + 1].Substring(ZLoc + 2, Lines[s + 1].Length - ZLoc - 3));

                        Vector3 RotationVector = new Vector3();
                        XLoc = Lines[s + 2].IndexOf('X');
                        YLoc = Lines[s + 2].IndexOf('Y');
                        ZLoc = Lines[s + 2].IndexOf('Z');
                        RotationVector.X = float.Parse(Lines[s + 2].Substring(XLoc + 2, YLoc - XLoc - 3));
                        RotationVector.Y = float.Parse(Lines[s + 2].Substring(YLoc + 2, ZLoc - YLoc - 3));
                        RotationVector.Z = float.Parse(Lines[s + 2].Substring(ZLoc + 2, Lines[s + 2].Length - ZLoc - 3));

                        LevelFinish = new Prop("LevelFinish.txt", PositionVector, RotationVector);
                        Props.Add(LevelFinish);
                    }
                    else if (Lines[s].StartsWith("LevelMusic = "))
                    {
                        LevelMusic = Lines[s].Substring("LevelMusic = ".Length);
                    }
                    else if (Lines[s].StartsWith("Props = "))
                    {
                        int NumOfProps = int.Parse(Lines[s].Substring("Props = ".Length));
                        for (int p = 0; p < NumOfProps; p++)
                        {
                            string PropName = Lines[s + 3 * p + 1];

                            Vector3 PositionVector = new Vector3();
                            int XLoc, YLoc, ZLoc;
                            XLoc = Lines[s + 3 * p + 2].IndexOf('X');
                            YLoc = Lines[s + 3 * p + 2].IndexOf('Y');
                            ZLoc = Lines[s + 3 * p + 2].IndexOf('Z');
                            PositionVector.X = float.Parse(Lines[s + 3 * p + 2].Substring(XLoc + 2, YLoc - XLoc - 3));
                            PositionVector.Y = float.Parse(Lines[s + 3 * p + 2].Substring(YLoc + 2, ZLoc - YLoc - 3));
                            PositionVector.Z = float.Parse(Lines[s + 3 * p + 2].Substring(ZLoc + 2, Lines[s + 3 * p + 2].Length - ZLoc - 3));

                            Vector3 RotationVector = new Vector3();
                            XLoc = Lines[s + 3 * p + 3].IndexOf('X');
                            YLoc = Lines[s + 3 * p + 3].IndexOf('Y');
                            ZLoc = Lines[s + 3 * p + 3].IndexOf('Z');
                            RotationVector.X = float.Parse(Lines[s + 3 * p + 3].Substring(XLoc + 2, YLoc - XLoc - 3));
                            RotationVector.Y = float.Parse(Lines[s + 3 * p + 3].Substring(YLoc + 2, ZLoc - YLoc - 3));
                            RotationVector.Z = float.Parse(Lines[s + 3 * p + 3].Substring(ZLoc + 2, Lines[s + 3 * p + 3].Length - ZLoc - 3));

                            Prop P = new Prop(PropName, PositionVector, RotationVector);
                            Props.Add(P);
                        }
                        s += 3 * NumOfProps;
                    }
                }
            }
        }
    }
}
